package com.wowoai.dialog.repository.impl;

import com.github.wenhao.jpa.PredicateBuilder;
import com.github.wenhao.jpa.Specifications;
import com.wowoai.common.reponsitory.DataJpaBaseDao;
import com.wowoai.dialog.entity.DialogHistoryEntity;
import com.wowoai.dialog.repository.DialogHistoryRepositoryI;
import com.wowoai.dialog.repository.jpa.DialogHistoryJpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public class DialogHistoryRepositoryImpl extends DataJpaBaseDao<DialogHistoryEntity, Long> implements DialogHistoryRepositoryI {

    private DialogHistoryJpaRepository dialogHistoryJpaRepository;

    public DialogHistoryRepositoryImpl(DialogHistoryJpaRepository dataJpaRepository) {
        super(dataJpaRepository);
        dialogHistoryJpaRepository = dataJpaRepository;
    }

    @Override
    public DialogHistoryEntity findByAnswer(String taskId) {

        PredicateBuilder<DialogHistoryEntity> builder = Specifications.and();
        builder.eq("deleted", Boolean.FALSE);
        builder.eq("answer", taskId);
        return dialogHistoryJpaRepository.findOne(builder.build()).orElse(null);
    }
}
